package com.zat.mes.andrmes.dofun.work.fragment

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.zat.mes.andrmes.BaseApplication
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseFragment
import com.zat.mes.andrmes.data.ClassResult
import com.zat.mes.andrmes.dofun.work.adapter.VpFragmentAdapter
import com.zat.mes.andrmes.dofun.work.vm.WorkViewModel
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.fragment_day_p_plan_classlist.*

/**
 * @author zy
 * @date :2020-02-25 15:38
 * @description:做工计划班组fragment
 */
class DayPplanClassListFragment : BaseFragment() {

    private lateinit var vpAdapter: VpFragmentAdapter
    private var empId: Long = 0
    private var parentorgids: String = ""
    private var listFragment: MutableList<BaseFragment> = ArrayList()
    private var list: MutableList<ClassResult> = ArrayList()

    companion object {
        private val EMPID = "empId"
        private val IDS = "parentorgids"
        fun newInstance(empId: Long, ids: String): DayPplanClassListFragment {
            val fragment = DayPplanClassListFragment()
            val args = Bundle()
            args.putLong(EMPID, empId)
            args.putString(IDS, ids)
            fragment.arguments = args
            return fragment
        }
    }

    override fun getViewId(): Int {
        return R.layout.fragment_day_p_plan_classlist
    }

    override fun isOpenTitleBar(): Boolean {
        return false
    }

    override fun initView() {
    }

    /**
     * 初始化数据
     */
    override fun initData() {

        parentorgids = arguments?.getString(IDS).toString()
        vpAdapter = VpFragmentAdapter(this)
        vpAdapter.listFragment = listFragment
        vpClassList.adapter = vpAdapter
        tabClassList.tabGravity = TabLayout.GRAVITY_START

        TabLayoutMediator(
            tabClassList,
            vpClassList,
            object : TabLayoutMediator.TabConfigurationStrategy {
                override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
                    tab.text = list.get(position).orgname
                }
            }).attach()

        initModel()

    }

    /**
     * 初始化model
     */
    private fun initModel() {

        val workViewModel: WorkViewModel = ViewModelProvider(this).get(WorkViewModel::class.java)

        workViewModel.getOrgClass().observe(this, androidx.lifecycle.Observer {
            closeLoading()
            list = it
            it.forEach {
                listFragment.add(DayPplanEmpListFragment.newInstance(empId, it.orgid))
            }
            vpAdapter.notifyDataSetChanged()

        })
        workViewModel.getMessageData().observe(this, androidx.lifecycle.Observer {
            closeLoading()
             Toast.makeText(activity, it, Toast.LENGTH_SHORT).show()
        })
        workViewModel.loadClassList(parentorgids)
        showLoading()

    }


}